require "import"
import "android.app.*"
import "android.os.*"
import "android.widget.*"
import "android.view.*"
import "layout"
import "android.content.Context"
import "android.media.ImageReader"
import "android.util.DisplayMetrics"
import "android.graphics.Bitmap"
import "android.content.Intent"
import "android.provider.Settings"
import "com.demo.MainActivity"
import "json"
activity.setContentView(loadlayout(layout))
p=activity.getSystemService(Context.MEDIA_PROJECTION_SERVICE)
activity.startActivityForResult(p.createScreenCaptureIntent(),6556)
imageR=nil
time=activity.getSharedData("time",500)
et.text=tostring(time)
auth=tostring(MainActivity.getAuth())
function onActivityResult(req,res,data)
  if req==6556 and res==-1 and data~=nil then
    local win=activity.getSystemService(Context.WINDOW_SERVICE)
    local metrics = DisplayMetrics()
    win.getDefaultDisplay().getMetrics(metrics)
    local projection=p.getMediaProjection(-1,data)
    imageR=ImageReader.newInstance(activity.getWidth(),activity.getHeight(),1,1)
    projection.createVirtualDisplay("screen-mirror",activity.getWidth(),activity.getHeight(),metrics.densityDpi,16,imageR.getSurface(),nil,nil)
  end
end
function savePic(bitmap)
  if bitmap ~= nil then
    local file=File("/sdcard/1.png")
    if not file.getParentFile().exists() then
      file.getParentFile().mkdirs()
    end
    if not file.exists() then
      file.createNewFile()
    end
    local fos = FileOutputStream(file)
    bitmap.compress(Bitmap.CompressFormat.PNG, 100, fos);
    fos.flush();
    fos.close();
  end
end
function getPic()
  if imageR~=nil then
  local image= imageR.acquireLatestImage()
  local planes=image.getPlanes()
  local bitmap = Bitmap.createBitmap(image.getWidth() + (planes[0].getRowStride() - (planes[0].getPixelStride()) * image.getWidth()) / (planes[0].getPixelStride()), image.getHeight(), Bitmap.Config.ARGB_8888);
  bitmap.copyPixelsFromBuffer(planes[0].getBuffer());
  bitmap = Bitmap.createBitmap(bitmap,0,0,image.getWidth(),image.getHeight());
  image.close();
  return bitmap;
  else
  return nil
  end
end
open.onClick=function()
  activity.startActivity( Intent(Settings.ACTION_ACCESSIBILITY_SETTINGS))
end
set.onClick=function()
  if tonumber(et.text)~=nil then
    activity.setSharedData("time",tonumber(et.text))
    time=tonumber(et.text)
    print("已设置并更新时间")
   else
    print("请输入数字")
  end
end
function onAccessibilityEvent(e)
  if tostring(e.getPackageName())=="com.beilei.app" then
    local yzm=e.getSource().findAccessibilityNodeInfosByViewId("com.beilei.app:id/img_code")[0]
    local rect= yzm.getBoundsInScreen()
    local edit=e.getSource().findAccessibilityNodeInfosByViewId("com.beilei.app:id/edt_code_image")[0]
    local enter=e.getSource().findAccessibilityNodeInfosByViewId("com.beilei.app:id/tv_exit_determine")[0]
    task(time,function()
      bit=getPic()
      if bit~=nil then
      savePic(Bitmap.createBitmap(bit, rect.left,rect.top,rect.right-rect.left,rect.bottom-rect.top))
      local back=MainActivity.webImage("/sdcard/1.png",auth)
      back= json.decode(back).words_result
      if #back~=0 then
        back=back[1].words
        local a=back:sub(2,2)
        local res=nil
        if tonumber(a)==nil then--是符号
          if a=="+" or a=="＋" then
            res=tonumber(back:sub(1,1))+ tonumber(back:sub(3,3))
           elseif
            a=="*" or a=="×" then
            res=tonumber(back:sub(1,1))*tonumber(back:sub(3,3))
           elseif a=="-" or a=="－" then
            res=tonumber(back:sub(1,1))-tonumber(back:sub(3,3))
           else
            res=tonumber(back:sub(1,1))/tonumber(back:sub(3,3))
          end
         else--是-号且就两个数
          res=tonumber(back:sub(1,1))-tonumber(back:sub(2,2))
        end
        res=tostring(res)
        arguments = Bundle();
        arguments.putCharSequence("ACTION_ARGUMENT_SET_TEXT_CHARSEQUENCE",res);
        if edit.performAction(2097152, arguments) then
          enter.performAction(16)
        end
       else
        print("验证码读取失败，请调整时间")
      end
    
    end
    
    end)
  end
end